<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="账套号" prop="sysAccount">
        <el-input
          v-model="queryParams.sysAccount"
          placeholder="请输入账套号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="产品编号" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入原价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售价" prop="salePrice">
        <el-input
          v-model="queryParams.salePrice"
          placeholder="请输入销售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货地" prop="shipPlace">
        <el-input
          v-model="queryParams.shipPlace"
          placeholder="请输入发货地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['api:product:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['api:product:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:product:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['api:product:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="productList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" min-width="120" show-overflow-tooltip /> -->
      <!-- <el-table-column label="账套号" align="center" prop="sysAccount" min-width="100" /> -->
      <el-table-column
        label="产品编号"
        align="center"
        prop="productNo"
        min-width="100"
      />
      <el-table-column
        label="产品名称"
        align="center"
        prop="productName"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        label="品牌"
        align="center"
        prop="brand"
        min-width="120"
        show-overflow-tooltip
      />
      <!-- <el-table-column label="标签" align="center" prop="label" min-width="80" /> -->
      <!-- <el-table-column label="型号/规格" align="center" prop="productSpec" min-width="100" /> -->
      <el-table-column
        label="产品简介"
        align="center"
        prop="introduce"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column label="缩略图" align="center" prop="converImageUrl">
        <template slot-scope="scope">
          <div>
            <el-image
              style="width: 50px; height: 50px; border-radius: 4px"
              :src="scope.row.converImageUrl"
              :preview-src-list="[scope.row.converImageUrl]"
              fit="cover"
              preview-teleported="true"
            />
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="详情图" align="center" prop="listPic">
        <template slot-scope="scope">
          <div>
            <el-image v-if="scope.row.listPicImgs" style="width: 50px; height: 50px; border-radius: 4px"
              :src="scope.row.listPicImgs[0]" :preview-src-list="scope.row.listPicImgs" fit="cover"
              preview-teleported="true" />
          </div>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="条码" align="center" prop="barcode" min-width="120" /> -->
      <el-table-column
        label="原价"
        align="center"
        prop="price"
        min-width="120"
        sortable
      />
      <el-table-column
        label="销售价"
        align="center"
        prop="salePrice"
        min-width="120"
        sortable
      />
      <el-table-column
        label="库存"
        align="center"
        prop="stock"
        min-width="120"
        sortable
      />
      <el-table-column
        label="单位"
        align="center"
        prop="unit"
        min-width="140"
      />
      <el-table-column label="规格类型" align="center" prop="specType">
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.specType == "0"
              ? "单规格"
              : scope.row.specType == "1"
              ? "多规格"
              : ""
          }}</span> -->
          <!-- <span>{{ dictDataChange("spec_type", scope.row.specType) }}</span> -->
          <dict-tag
            :options="dict.type.spec_type"
            :value="scope.row.specType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="产品分类id"
        align="center"
        prop="categoryId"
        min-width="100"
      />
      <el-table-column
        label="分类名称"
        align="center"
        prop="categoryName"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="发货地"
        align="center"
        prop="shipPlace"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="商品销量"
        align="center"
        prop="saleCnt"
        min-width="100"
        sortable
      />
      <el-table-column
        label="重量(KG)"
        align="center"
        prop="weight"
        min-width="100"
        sortable
      />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sale_cnt" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createDt"
        min-width="160"
      />

      <!-- <el-table-column label="升级值" align="center" prop="upgradeValue" min-width="100" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:product:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:product:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <!-- <el-form-item label="账套号" prop="sysAccount">
          <el-input v-model="form.sysAccount" placeholder="请输入账套号" />
        </el-form-item> -->
        <el-form-item label="产品编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <!-- <el-form-item label="标签" prop="label">
          <el-input v-model="form.label" placeholder="请输入标签" />
        </el-form-item> -->
        <el-form-item label="型号/规格" prop="productSpec">
          <el-input v-model="form.productSpec" placeholder="请输入型号/规格" />
        </el-form-item>
        <el-form-item label="产品简介" prop="introduce">
          <el-input
            type="textarea"
            v-model="form.introduce"
            placeholder="请输入产品简介"
          />
        </el-form-item>
        <el-form-item label="封面图" prop="converImageUrl">
          <ImageUpload :limit="1" v-model="form.converImageUrl" />
        </el-form-item>
        <!-- <el-form-item label="首页轮播图" prop="listPic">
          <ImageUpload :limit="9" v-model="proHomePicCarousel" />
        </el-form-item> -->
        <el-form-item label="商品轮播图" prop="listPic">
          <ImageUpload :limit="9" v-model="proListPicCarousel" />
        </el-form-item>
        <el-form-item label="详情图" prop="listPic">
          <ImageUpload :limit="9" v-model="formListPic" />
        </el-form-item>
        <!-- <el-form-item label="条码" prop="barcode">
          <el-input v-model="form.barcode" placeholder="请输入条码" />
        </el-form-item> -->
        <el-form-item label="原价" prop="price">
          <el-input v-model="form.price" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="销售价" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入销售价" />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位【库存单位】" />
        </el-form-item>
        <!-- <el-form-item label="产品分类id" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入产品分类id" />
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入分类名称" />
        </el-form-item> -->
        <el-form-item label="产品分类" prop="productType">
          <el-select
            v-model="form.categoryId"
            clearable
            placeholder="请选择"
            style="width: 100%"
            @change="categoryChange"
          >
            <el-option
              :label="item.categoryName"
              :value="item.id"
              v-for="item in categoryList"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发货地" prop="shipPlace">
          <el-input v-model="form.shipPlace" placeholder="请输入发货地" />
        </el-form-item>
        <el-form-item label="商品销量" prop="saleCnt">
          <el-input v-model="form.saleCnt" placeholder="请输入商品销量" />
        </el-form-item>
        <el-form-item label="重量(KG)" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量(KG)" />
        </el-form-item>
        <!-- <el-form-item label="升级值" prop="upgradeValue">
          <el-input v-model="form.upgradeValue" placeholder="请输入升级值" />
        </el-form-item> -->
        <el-form-item label="商品类型" prop="productType">
          <el-select
            v-model="form.productType"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.product_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="运费计算类型" prop="freightCalType">
          <el-select
            v-model="form.freightCalType"
            clearable
            placeholder="请选择运费计算类型"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.freight_cal_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="商品是否解冻上级库存" prop="thawTag">
          <el-select
            v-model="form.thawTag"
            clearable
            placeholder="请选择否解冻"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.thaw_tag"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="form.status"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sale_cnt"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="规格类型" prop="specType">
          <el-select
            v-model="form.specType"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.spec_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label=" "
          prop="listBasProductSku"
          v-if="form.specType == 1"
        >
          <el-button
            type="primary"
            plain
            icon="Plus"
            class="mb10"
            @click="toAddProType"
            >添加商品规格</el-button
          >
          <el-table
            :data="form.listBasProductSku"
            v-if="form.listBasProductSku.length > 0"
          >
            <el-table-column
              label="sku名称"
              align="center"
              prop="skuName"
              min-width="100"
            >
              <template slot-scope="scope">
                <el-input
                  placeholder="请输入"
                  v-model.trim="scope.row.skuName"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="封面图"
              align="center"
              prop="coverImage"
              min-width="120"
            >
              <template slot-scope="scope">
                <ImageUpload
                  :limit="1"
                  v-model="scope.row.coverImage"
                  class="skuCoverImg"
                  :isShowTip="false"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="sku库存"
              align="center"
              prop="stock"
              min-width="100"
            >
              <template slot-scope="scope">
                <el-input placeholder="请输入" v-model.trim="scope.row.stock" />
              </template>
            </el-table-column>
            <el-table-column
              label="sku销售价格"
              align="center"
              prop="salePrice"
              min-width="100"
            >
              <template slot-scope="scope">
                <el-input
                  placeholder="请输入"
                  v-model.trim="scope.row.salePrice"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="sku重量"
              align="center"
              prop="weight"
              min-width="100"
            >
              <template slot-scope="scope">
                <el-input
                  placeholder="请输入"
                  v-model.trim="scope.row.weight"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="sku单位"
              align="center"
              prop="unit"
              min-width="100"
            >
              <template slot-scope="scope">
                <el-input placeholder="请输入" v-model.trim="scope.row.unit" />
              </template>
            </el-table-column>
            <el-table-column
              label="数量比率"
              align="center"
              prop="qtyRate"
              min-width="100"
            >
              <template slot-scope="scope">
                <el-input
                  placeholder="请输入"
                  v-model.trim="scope.row.qtyRate"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="排序"
              align="center"
              prop="sort"
              min-width="100"
            >
              <template slot-scope="scope">
                <el-input placeholder="请输入" v-model.trim="scope.row.sort" />
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  type="danger"
                  size="small"
                  @click="toDeleteProType(scope)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end"
      >
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
} from "@/api/djk/mallMgr/product";
import { listCategory } from "@/api/djk/mallMgr/category";

export default {
  name: "Product",
  dicts: [
    "spec_type",
    "sale_cnt",
    "product_type",
    "freight_cal_type",
    "thaw_tag",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysAccount: null,
        productNo: null,
        productName: null,
        price: null,
        salePrice: null,
        specType: null,
        categoryName: null,
        shipPlace: null,
        status: null,
        createDt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      categoryList: [],
      rules: {},
      formListPic: "", //详情详情图
      proListPicCarousel: "", //商品轮播图
      // proHomePicCarousel: "", //首页轮播图
    };
  },
  created() {
    this.getList();
  },
  methods: {
    toAddProType() {
      this.form.listBasProductSku.push({
        skuName: "",
        stock: 0,
        salePrice: 0,
        weight: 0,
        unit: "",
        qtyRate: "",
        coverImage: "",
        sort: this.form.listBasProductSku.length + 1,
      });
    },
    toDeleteProType({ $index }) {
      this.form.listBasProductSku.splice($index, 1);
    },
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        // let listPicImgs = [];
        // if (this.productList.listPic) {
        //   this.productList.listPic.forEach((item) => {
        //     if(item.showType == 1) {
        //       listPicImgs.push(item.resUrl);
        //     }
        //   });
        // }
        // this.productList.listPicImgs = listPicImgs;
        this.total = response.total;
        this.loading = false;
      });
      listCategory({
        pageNum: 1,
        pageSize: 20,
      }).then((response) => {
        this.categoryList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sysAccount: null,
        productNo: null,
        productName: null,
        brand: null,
        label: null,
        productSpec: null,
        introduce: null,
        converImageUrl: null,
        listPic: [],
        listBasProductSku: [],
        barcode: null,
        price: null,
        salePrice: null,
        stock: null,
        unit: null,
        specType: null,
        categoryId: null,
        categoryName: null,
        shipPlace: null,
        saleCnt: null,
        status: null,
        createDt: null,
        createBy: null,
        weight: null,
        productType: null,
        upgradeValue: null,
      };
      this.formListPic = "";
      this.proListPicCarousel = "";
      this.resetForm("form");
    },
    categoryChange(val) {
      this.categoryList.forEach((item) => {
        if (item.id == val) {
          this.form.categoryName = item.categoryName;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProduct(id).then((response) => {
        let listPic = response.data.listPic,
          formListPic = [],
          proListPicCarousel = [];
        // proHomePicCarousel = [];
        listPic.forEach((item) => {
          if (item.showType == 1) {
            formListPic.push(item.resUrl);
          } else if (item.showType == 2) {
            proListPicCarousel.push(item.resUrl);
          }
          // else if (item.showType == 3) {
          //   proHomePicCarousel.push(item.resUrl);
          // }
        });
        this.formListPic = formListPic.join(",");
        this.proListPicCarousel = proListPicCarousel.join(",");
        // this.proHomePicCarousel = proHomePicCarousel.join(",");
        this.form = response.data;
        if (!this.form.listBasProductSku) {
          this.form.listBasProductSku = [];
        }
        this.open = true;
        this.title = "修改产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let imgList1 = this.formListPic.split(",");
          let imgList2 = this.proListPicCarousel.split(",");
          // let imgList3 = this.proHomePicCarousel.split(",");
          let listPic = [];
          if (imgList1[0] != "") {
            imgList1.forEach((item, idx) => {
              listPic.push({
                resType: "1",
                showType: "1",
                resUrl: item,
                sort: listPic.length + 1,
              });
            });
          }
          if (imgList2[0] != "") {
            imgList2.forEach((item, idx) => {
              listPic.push({
                resType: "1",
                showType: "2",
                resUrl: item,
                sort: listPic.length + 1,
              });
            });
          }
          // imgList3.forEach((item, idx) => {
          //   listPic.push({
          //     resType: "1",
          //     showType: "3",
          //     resUrl: item,
          //     sort: listPic.length + 1,
          //   });
          // });
          this.form.listPic = listPic;

          if (this.form.id != null) {
            // imgList.forEach((item, idx) => {
            //     if (item != this.form.listPic[idx].resUrl) {
            //       this.form.listPic[idx].resUrl = item;
            //     }
            //   });

            updateProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProduct(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "api/product/export",
        {
          ...this.queryParams,
        },
        `product_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped lang="scss">
.skuCoverImg {
  &::v-deep .el-upload--picture-card {
    width: 60px;
    height: 60px;
    line-height: 72px;
  }

  &::v-deep .el-upload-list--picture-card .el-upload-list__item {
    width: 60px;
    height: 60px;
    line-height: 72px;
  }

  &::v-deep
    .el-upload-list__item.is-success
    .el-upload-list__item-status-label {
    display: none;
  }
}
</style>
